package com.yujistu;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.loopj.android.http.AsyncHttpClient;
import com.yujistu.integration.result.GetPubTimeResult;
import com.yujistu.integration.bean.UserEntity;
import com.yujistu.integration.bean.VerifyInfoEntity;
import com.yujistu.integration.request.GetPubTimeRequest;
import com.yujistu.network.StuRestClient;
import com.yujistu.utils.map.MyLocationListener;

/**
 * Created by S3YOGA on 2015/11/3.
 */
public class App extends Application {

    public static App mInstance = null;

    public String  token;

    public UserEntity  currentUser;
    public VerifyInfoEntity mVerifyInfo;

    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();

    public static App getInstance(){
        if (null == mInstance) {
            try {
                throw new Exception("Invalid app status");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        init();
    }

    private void init() {
        AsyncHttpClient client = new AsyncHttpClient();
        StuRestClient.setHttpClient(client);

        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);    //注册监听函数
        initLocation();
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span=1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认false，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        if(!TextUtils.isEmpty(token)){
            getPubTime();
        }
    }

    private void getPubTime() {
        GetPubTimeRequest request = new GetPubTimeRequest();

    }

    private GetPubTimeResult mGetPubTimeResult;

    public GetPubTimeResult getmGetPubTimeResult() {
        return mGetPubTimeResult;
    }

    public void setmGetPubTimeResult(GetPubTimeResult mGetPubTimeResult) {
        this.mGetPubTimeResult = mGetPubTimeResult;
    }

    public VerifyInfoEntity getmVerifyInfo() {
        return mVerifyInfo;
    }

    public void setmVerifyInfo(VerifyInfoEntity mVerifyInfo) {
        if (null != mVerifyInfo) {
            this.mVerifyInfo = mVerifyInfo;
        }
    }

    static Context _context;
    static Resources _resource;
    private static String lastToast = "";
    private static long lastToastTime;

    private static boolean sIsAtLeastGB;

    public static void showToastShort(String msg) {
        showToast(msg, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
    }

    private static void showToast(String msg, int duration, int icon, int gravity) {
        if(msg != null && !msg.equalsIgnoreCase(" ")){
            long time = System.currentTimeMillis();
            if(!msg.equalsIgnoreCase(lastToast) || Math.abs(time - lastToastTime) > 2000){
                View view = LayoutInflater.from(_context).inflate(R.layout.view_toast, null);
                ((TextView)view.findViewById(R.id.title_tv)).setText(msg);
                if(icon != 0){
                    ((ImageView)view.findViewById(R.id.icon_iv)).setImageResource(icon);
                    ((ImageView)view.findViewById(R.id.icon_iv)).setVisibility(View.VISIBLE);
                }
                Toast toast = new Toast(_context);
                toast.setView(view);
                if(gravity == Gravity.TOP){
                    toast.setGravity(gravity, 0, 0);
                }else{
                    toast.setGravity(gravity, 0, 35);
                }

                toast.setDuration(duration);
                toast.show();
                lastToast = msg;
                lastToastTime = System.currentTimeMillis();
            }
        }
    }
}
